import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

export function splitStr(str){
  let s = str.toString().replace(":","");
  return Number(s)
}

export function compareTime(timeList){
  //当前时间
  let date = new Date();
  let hour = date.getHours();
  let minutes = date.getMinutes();
  let items = ''
  timeList.forEach(item => {
    let indexStart = item.startTime.toString().indexOf(":")
    let indexEnd = item.endTime.toString().indexOf(":")
    //时
    let hourStartPre = Number(item.startTime.toString().substring(0,indexStart))
    let hourEndPre = Number(item.endTime.toString().substring(0,indexEnd))
    //分
    let minutesStartLate = item.startTime.toString().substr(indexStart+1,2)
    let minutesEndLate = item.endTime.toString().substr(indexEnd+1,2)
    if (hour >= hourStartPre && hour < hourEndPre){
      console.log("start  "+hourStartPre+"   end "+hourEndPre)
      console.log("item  "+JSON.stringify(item))
      items = item
    }
  })
  return items;
}

//日期加n个小时
export function dayAdd(date){
  return new Date(Date.parse(new Date(date))+8*3600*1000)
}

export function isNumber(number){
  return !isNaN(Number(number))
}

/**
 * 保留两位小数
 * @param num
 */
export function keepTwoPoint(num){
  return Math.round(Number(num)*100)/100
}

/**
 * 小数点前面的数字
 * @param num
 * @returns {number}
 */
export function pointPreNum(num){
  return parseInt(num)
}

/**
 * 小数点后两位
 * @param num
 * @returns {number}
 */
export function pointBehindNum(num){
  var numStr = num.toString();
  var index = numStr.indexOf(".")
  return Number(numStr.substr(index + 2, 1))
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('token')
  Vue.cookie.delete('seller')
  Vue.cookie.delete("sellerInfo")
  Vue.cookie.delete('zeroStatus')
  Vue.cookie.delete('rule')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}
